﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Application
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Forms;

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1601:PartialElementsMustBeDocumented", Justification = "Another part already documents the class.")]
    internal partial class MainForm
    {
        /// <summary>Provides methods common to all non-trivial controllers.</summary>
        private abstract class Controller
        {
            private readonly MainForm mainForm;

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            /// <summary>See <see cref="IDisposable.Dispose"/>.</summary>
            public void Dispose()
            {
                this.Dispose(true);
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            internal MainForm MainForm
            {
                get { return this.mainForm; }
            }

            internal void ClearTreeViewNodes()
            {
                this.MainForm.treeView.AfterSelect -= this.OnTreeView_AfterSelect;

                try
                {
                    // It seems this call removes the nodes one by one. Removing the currently selected node
                    // automatically selects a different one. So, in the worst case AfterSelect is fired for each
                    // node in the tree, which is why we remove all handlers before clearing.
                    this.MainForm.treeView.Nodes.Clear();
                }
                finally
                {
                    // We want the AfterSelect handler to run when we add the previously selected article
                    this.MainForm.treeView.AfterSelect += this.OnTreeView_AfterSelect;
                }
            }

            internal abstract void OnQuickMenuItem_Click(object sender, EventArgs e);

            internal abstract void OnTreeView_AfterSelect(object sender, TreeViewEventArgs e);

            internal abstract void OnTreeView_KeyDown(object sender, KeyEventArgs e);

            internal abstract void OnTreeView_NodeTap(object sender, EventArgs e);

            internal abstract void OnContextMenu_Popup(object sender, EventArgs e);

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            /// <summary>Initializes a new instance of the <see cref="Controller"/> class.</summary>
            protected Controller(MainForm mainForm)
            {
                this.mainForm = mainForm;

                this.MainForm.quickMenuItem.Enabled = false;
                this.mainForm.quickMenuItem.Click += this.OnQuickMenuItem_Click;

                this.mainForm.treeView.AfterSelect += this.OnTreeView_AfterSelect;
                this.mainForm.treeView.KeyDown += this.OnTreeView_KeyDown;
                this.mainForm.treeView.NodeTap += this.OnTreeView_NodeTap;

                this.mainForm.contextMenu.Popup += this.OnContextMenu_Popup;
            }

            /// <summary>Unsubscribes the <see cref="Controller"/> instance from the events it was hooked up with
            /// during construction.</summary>
            protected virtual void Dispose(bool disposing)
            {
                this.mainForm.contextMenu.Popup -= this.OnContextMenu_Popup;
                this.mainForm.contextMenu.MenuItems.Clear();

                this.mainForm.treeView.NodeTap -= this.OnTreeView_NodeTap;
                this.mainForm.treeView.KeyDown -= this.OnTreeView_KeyDown;
                this.mainForm.treeView.AfterSelect -= this.OnTreeView_AfterSelect;

                this.mainForm.quickMenuItem.Click -= this.OnQuickMenuItem_Click;
            }
        }
    }
}
